# -*-coding: Utf-8 -*-
# @File : app .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2023/10/9
import re

from views.house import House
from views.user import User
from flask import Flask, request, render_template, redirect, session
from database import MysqlController
from main_spider.spider import Spider
from settings import base_settings, server_settings, config

spider = Spider()
mysql_handler = MysqlController()
application = Flask(__name__)
application.secret_key = '1314521'  # 设置密钥，用于加密Session数据
application.register_blueprint(User.user_view)
application.register_blueprint(House.house_view)


@application.route('/')
def index():
    return redirect('/house/house_data')


@application.route('/edit/update', methods=['POST'])
def update_house_data():
    data = request.form


@application.before_request
def before_request():
    request.mysql_handler = mysql_handler
    request.spider = spider
    pat = re.compile(r'^/static')
    if re.search(pat, request.path):
        return
    if request.path == '/user/login':
        return
    if request.path == '/user/register':
        return
    username = session.get('username')
    if not username:
        return redirect('/user/login')
    else:
        return


def run():
    application.run(host=config.SERVER_HOST, port=config.SERVER_PORT, debug=config.DEBUG)


if __name__ == '__main__':
    run()
